//
//  DateUtil.m
//  Rong360
//
//  Created by tianliwei on 14-2-18.
//  Copyright (c) 2014年 tianliwei. All rights reserved.
//

#import "DateUtil.h"

@implementation DateUtil
+(int)compareOneDay:(NSDate *)oneDay withAnotherDay:(NSDate *)anotherDay
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"dd-MM-yyyy"];
    NSString *oneDayStr = [dateFormatter stringFromDate:oneDay];
    NSString *anotherDayStr = [dateFormatter stringFromDate:anotherDay];
    NSDate *dateA = [dateFormatter dateFromString:oneDayStr];
    NSDate *dateB = [dateFormatter dateFromString:anotherDayStr];
    NSComparisonResult result = [dateA compare:dateB];
    NSLog(@"date1 : %@, date2 : %@", oneDay, anotherDay);
    if (result == NSOrderedDescending) {
        //NSLog(@"Date1  is in the future");
        return 1;
    }
    else if (result == NSOrderedAscending){
        //NSLog(@"Date1 is in the past");
        return -1;
    }
    //NSLog(@"Both dates are the same");
    return 0;
}
+ (NSString *)stringWithDate:(NSDate *)date{
    NSDateFormatter *selectDateFormatter = [[NSDateFormatter alloc] init];
    selectDateFormatter.dateFormat = @"yy-MM-dd"; // 设置日期的格式
    NSString *dateStr= [selectDateFormatter stringFromDate:date]; // 把date类型转为设置好格式的
    return dateStr;
}
+ (NSString *)addTimeFromDate:(NSDate *)fromDate addMonths:(NSInteger)months{
    NSCalendar *calendar = [NSCalendar currentCalendar];
	NSDateComponents *componentsToAdd = [[NSDateComponents alloc] init];
	[componentsToAdd setMonth:months];
	NSDate *dateAfterMonth = [calendar dateByAddingComponents:componentsToAdd toDate:fromDate options:0];
	
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yy-MM-dd"];
    NSString *titleString = [dateFormatter stringFromDate:dateAfterMonth];
    return titleString;
}
+ (BOOL)firstDate:(NSDate *)firstDate isLargeThanSecondDate:(NSDate *)secondDate{
    NSTimeInterval secondsInterval= [firstDate timeIntervalSinceDate:secondDate];
    if (secondsInterval > 0) {
        return YES;
    }
    return NO;
}
@end
